package formula.web;

import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import org.primefaces.component.datatable.DataTable;
import org.slf4j.LoggerFactory;

import twork.jsf.util.TworkJsfMessageUtil;
import formula.business.dao.DriverDao;
import formula.business.model.Driver;

@SuppressWarnings("serial")
@ManagedBean
@ViewScoped
public class DriversView implements Serializable {

	private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

	@ManagedProperty(value = DriverHome.EL_NAME)
	DriverHome driverHome;
	@ManagedProperty(value = DriversHome.EL_NAME)
	DriversHome driversHome;
	@ManagedProperty(value = DriverDao.EL_NAME)
	private DriverDao driverDao;
	private static String DATABLE_UI_ID = "form:dataTable";
	private boolean ajaxAction = true;
	private boolean editMode = false;
	/**
	 * linked to the multiple checkbox selection on PF datatable list on drivers.xhtml. I chose to
	 * manage the multiple selection directly by Object (Driver), i could have kept only the ids
	 */
	private Map<Driver, Boolean> selectedDrivers = new HashMap<Driver, Boolean>();

	public void clearSearchForm() {
		driversHome.clearDriverForm();
		resetDatatableValue(DATABLE_UI_ID);
	}

	public void search() {
		resetDatatableValue(DATABLE_UI_ID);
	}

	public void resetDatatableValue(String componentName) {
		UIComponent datatableComponent = FacesContext.getCurrentInstance().getViewRoot().findComponent(componentName);
		if (datatableComponent != null) {
			DataTable dataTable = (DataTable) datatableComponent;
			dataTable.reset();
			logger.debug("clear2");
		}
	}

	public boolean isAjaxAction() {
		return ajaxAction;
	}

	public void setAjaxAction(boolean _ajaxAction) {
		String msg = null;
		if ((_ajaxAction) && (!this.ajaxAction)) {
			msg = "ajax.action";
		} else if ((!_ajaxAction) && (this.ajaxAction)) {
			msg = "ajax.unaction";
		}
		if (msg != null)
			TworkJsfMessageUtil.addMessageInfo("appMsg", msg);
		ajaxAction = _ajaxAction;
	}

	public void cancelEditDriver() {
		driverHome.clear();
		this.editMode = false;
	}

	public void deleteDriver() {
		driverHome.doDelete();
		this.editMode = false;
	}

	public void updateDriver() {
		driverHome.doMerge();
		this.editMode = false;
	}

	public void setEditDriverById(Long _driverId) {
		driverHome.setDriverId(_driverId);
		editMode = true;
	}

	public void deleteAllSelectedDriver() {
		Set<Driver> keysets = selectedDrivers.keySet();
		Set<Driver> removedDrivers = new HashSet<Driver>();
		for (Driver p : keysets) {
			if (selectedDrivers.get(p) == true) {
				driverDao.delete(p);
				removedDrivers.add(p);
			}
		}
		if (removedDrivers.size() > 0) {
			String idmsg = "";
			for (Driver p : removedDrivers) {
				idmsg += p.getId() + ", ";
			}
			idmsg = idmsg.substring(0, idmsg.length() - 2) + " ";
			TworkJsfMessageUtil.addMessageInfo("appMsg", "success.deletes", removedDrivers.size() + " Drivers (id)",
					idmsg);
		} else {
			TworkJsfMessageUtil.addMessageInfo("appMsg", "no.selected", "Driver");
		}
		selectedDrivers.clear();
		logger.debug("delete finished");
	}

	// then below : simple setters and getters
	public void setDriverHome(DriverHome _driverHome) {
		driverHome = _driverHome;
	}

	public void setDriversHome(DriversHome _driversHome) {
		driversHome = _driversHome;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean _editMode) {
		editMode = _editMode;
	}

	public Map<Driver, Boolean> getSelectedDrivers() {
		return selectedDrivers;
	}

	public boolean getHasSelectedDrivers() {
		Set<Driver> keysets = this.selectedDrivers.keySet();
		for (Driver p : keysets) {
			if (this.selectedDrivers.get(p) == true) {
				return true;
			}
		}
		return false;
	}

	public void setSelectedDrivers(Map<Driver, Boolean> _selectedDrivers) {
		selectedDrivers = _selectedDrivers;
	}

	public void setDriverDao(DriverDao driverDao) {
		this.driverDao = driverDao;
	}
}
